namespace ScrumSprintMonitor.Wpf.Application
{
    #region #using Directives

    using System.Diagnostics.CodeAnalysis;
    using System.Windows;
    using System.Windows.Forms;

    using Design;

    using Implementation;

    using Infrastructure;
    using Infrastructure.Configuration;
    using Infrastructure.IoC;

    using Microsoft.Practices.Unity;

    using UI.Wpf;

    #endregion

    [SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses",
        Justification = "The UnityContainerAbstraction will create this class")]
    internal class UnityAppDesignTimeModule : StandardModule
    {
        /// <summary>
        ///   Loads the module into the container.
        /// </summary>
        public override void Load()
        {
            SetupDesignTimeBindings();
        }

        private void SetupDesignTimeBindings()
        {
            // TODO: Mock the following two concrete classes
            Container.RegisterType<IConnectionProvider, ConnectionProvider>(new ContainerControlledLifetimeManager());
            Container.RegisterType<IProcessTemplateAdapterProvider, MefProcessTemplateAdapterProvider>(new ContainerControlledLifetimeManager());
            Container.RegisterInstance(TestAssetLibrary.CreateSprintConfiguration());
            Container.RegisterType<IPresentationConfiguration, TestAssetLibrary.HardcodedPresentationConfiguration>(
                new ContainerControlledLifetimeManager());

            var shellLayoutConfiguration = new ShellLayoutConfiguration(SystemInformation.MonitorCount > 1, !Bootstrapper.Debug, true,
                                                                        SprintMode.Sprinting);
            Container.RegisterInstance<IShellLayoutConfiguration>(shellLayoutConfiguration);
        }
    }

    /// <summary>
    ///   Provides a custom implementation of DesignerProperties.GetIsInDesignMode
    ///   to work around an issue.
    /// </summary>
    internal static class StubDesignerProperties
    {
        /// <summary>
        ///   Stores the computed InDesignMode value.
        /// </summary>
        private static bool? isInDesignMode;

        /// <summary>
        ///   Returns whether the control is in design mode (running under Blend
        ///   or Visual Studio).
        /// </summary>
        /// <param name = "element">The element from which the property value is
        ///   read.</param>
        /// <returns>True if in design mode.</returns>
        [SuppressMessage("Microsoft.Usage", "CA1801:ReviewUnusedParameters", MessageId = "element",
            Justification =
                "Matching declaration of System.ComponentModel.DesignerProperties.GetIsInDesignMode (which has a bug and is not reliable).")]
        public static bool GetIsInDesignMode(DependencyObject element)
        {
            if (!isInDesignMode.HasValue)
            {
                isInDesignMode = (null == System.Windows.Application.Current) || !(System.Windows.Application.Current is App);
            }
            return isInDesignMode.Value;
        }
    }
}